<?php
class Referral_model extends MY_Model
{
	public
		$table = 'users_referrals'
		//$prKey = 'users_id',
		//$publishedField = 'faq_status',

	;

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'referral_no'           => T_('Referral No'),
			'status'                => T_('Status'),
			'date_posted'           => T_('Date Posted'),
			'end_date'              => T_('End Date'),
			'referral_abstract' 	=> T_('Referral Abstract'),
			
		);
			
		$this->rules = array(
			'referral_no'           => "trim|integer",
			'status'                => "trim|required|max_length[255]",
			'date_posted'           => "trim|required",
			'end_date'              => "trim|max_length[255]",
			'referral_abstract' 	=> "trim|required|is_natural_no_zero",
			'requests_id' 			=> "trim|integer",
			'requests' 	        	=> "trim|required|max_length[255]",
			'ur_date_posted'	    => "trim|max_length[255]",
		);
	}
    function getReferralDetails($referral){

        $userID=$this->auth->getID();
    	$sql = "SELECT *
    			FROM users_referrals
    			WHERE ur_user_ID='".$userID."' AND ur_ID=".$referral." LIMIT 1";
        $result = $this->db->query($sql)->row_array();
        return $result;
    }

    function search() {

       $userID = $this->auth->getID();
       $sql = "SELECT * FROM users_referrals
               WHERE ur_user_ID='".$userID."'";
       $result = $this->db->query($sql)->result_array();
       return $result;
    }
    function insert($data)
	{
		if (!empty($data))
		{
			parent::insert($data);

            if ($this->db->insert_id() > 0)
			{
                $this->log->writeMessage('done', T_('Request was added succesfully.'));

				return TRUE;
			}
			else
			{


				$this->log->writeMessage('error', T_('Error! Request was not created.'));

				return FALSE;
			}
		}
	}
function searchReferral($referral){
       $sql = "SELECT * FROM users_referrals
               WHERE ur_ID=".($referral*1)." LIMIT 1";//ur_user_ID='".$userID."' AND
       $result = $this->db->query($sql)->row_array();
       return $result;
}


function search_home_page()
	{	
		$this->db->from($this->table);
		$this->db->order_by("ur_date_posted", "desc");
		$this->db->limit(10);
		$this->data['list'] = $this->db->get()->result();
		
	}
function number_users()
	{	
		$sql = "SELECT COUNT(user_ID) AS number
				FROM users
				WHERE DATE(user_created) = DATE(NOW()) AND user_id_acl_roles != 3";
		$arr = $this->db->query($sql)->row_array();
		$this->data['user_number'] = $arr['number'];
	}
function get_id(){
    $sql = "SELECT MAX(ur_id) AS number
            FROM users_referrals";
    $id = $this->db->query($sql)->row_array();
    return $id['number']+1;
}
function changeField($field, $newValue, $condition = ""){
    $sql = "UPDATE ".$this->table."
            SET ".$field."='".$newValue."'
            WHERE ur_user_id=".$this->auth->getID()." ".$condition;
    $isok = $this->db->query($sql);
}
}
?>